import 'dart:convert';

import 'package:alipay_kit/alipay_kit.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:jd_shop/utils/ToastMsg.dart';

import '../../base/Constant.dart';
import '../../services/ScreenAdaper.dart';

class Pay extends StatefulWidget {
  Map arguments;

  Pay({super.key, required this.arguments});

  @override
  State<StatefulWidget> createState() {
    return _PayState();
  }
}

bool isSelectWx = true;
bool isSelectZfb = false;

class _PayState extends State<Pay> {
  @override
  void initState() {
    super.initState();
    _payListen();
  }

  _payListen() {
    // registerWxApi(appId: "wxcd9c4a1fe5079151", universalLink: "");
    // weChatResponseEventHandler.listen((event) {
    //   if (event is WeChatAuthResponse) {
    //     print('授权code,${event.code}');
    //   }
    //   if (event.errCode == 0) {
    //     print('支付成功,${event.errCode}');
    //   } else if (event.errCode == -2) {
    //     print('用户取消支付了');
    //   } else {
    //     print('支付失败原因：${event.errStr}');
    //   }
    // });
  }

  // 微信支付
  _payWeChat() {
    // payWithWeChat(
    //     appId: Constant.WX_PAY_APP_ID,
    //     //appid
    //     partnerId: '0003610F7886018',
    //     prepayId: '',
    //     packageValue: 'Sign=WXPay',
    //     //订单号
    //     nonceStr: '',
    //     //随机串
    //     timeStamp: '',
    //     //时间戳
    //     sign: '',
    //     //支付签名
    //     signType: 'MD5',
    //     extData: '选填'
    // );
  }

  String jsonString = '''
  {
    "code": 1,
    "data": {
      "code": null,
      "msg": null,
      "subCode": null,
      "subMsg": null,
      "body": "alipay_root_cert_sn=687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6&alipay_sdk=alipay-sdk-java-4.10.29.ALL&app_cert_sn=231eb1e017d7bb2d161e6455d6eb43fb&app_id=2015121700992149&biz_content=%7B%22body%22%3A%22%E8%B4%AD%E7%89%A9%22%2C%22out_trade_no%22%3A%22T20250107200232595746%22%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%2C%22subject%22%3A%22%E5%A4%A7%E6%9C%89%E8%B0%B7+%E7%9B%90%E7%94%B0%E8%99%BE40%E6%9D%A1%E8%A2%8B%E8%A3%85%E5%88%9B%E5%AE%A2%E4%B8%93%E4%BA%AB%22%2C%22timeout_express%22%3A%2230m%22%2C%22total_amount%22%3A%2279.05%22%7D&charset=UTF-8&format=json&method=alipay.trade.app.pay&notify_url=http%3A%2F%2F121.37.187.225%3A8081%2FShoppingPayment%2FpayNotify%2FaliPayNotify&sign=PAz4tEVwflcL5FctX%2BZGl9PKYBO9hbzHe8j4R27ATEhBi67U9Q%2Fgcdk4NxtzVQoJ84RnnDt3ippQtWne5Tsyo%2B2Eqg2f65bZueyYRZL%2F4qoqPUP68HAZ%2FFDMz5TA%2FrLFtSIksf0MkWpA7MLo9CsXt9a6GHwLwr3G2DTFV8Nx6CDz9gOecI%2F1d7Si%2Bgl1vZHA%2BCj%2B12uX4yKfgCIYIQY8qntXhkDh9CRz6790DZ3s9mtXIDJLAdsOebTjy0O66c7L1IT%2F3oJ2KvdmKQih%2Bb4aCp2mr6oKiZBmqD2rbzFgD2J%2BtnOoUCDbx52aVFRafZzFJKsHrquW6NFeOR8g58meOw%3D%3D&sign_type=RSA2&timestamp=2025-01-07+20%3A02%3A32&version=1.0",
      "params": null,
      "merchantOrderNo": null,
      "outTradeNo": null,
      "sellerId": null,
      "totalAmount": null,
      "tradeNo": null,
      "errorCode": null,
      "success": true
    },
    "desc": "操作成功"
  }
  ''';

  @override
  Widget build(BuildContext context) {
    ScreenAdapter.init(context);
    return Scaffold(
      appBar: AppBar(
        title: const Text('支付'),
      ),
      body: Column(
        children: [
          InkWell(
            child: Container(
              margin: const EdgeInsets.all(15),
              decoration: const BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.all(Radius.circular(10)),
              ),
              height: 60,
              child: Row(
                children: [
                  Image.asset("images/ic_wechat.png",
                      width: ScreenAdapter.height(40),
                      height: ScreenAdapter.height(40),
                      fit: BoxFit.cover),
                  const Expanded(
                    child: Text(
                      "微信支付",
                      textAlign: TextAlign.left,
                      style: TextStyle(fontSize: 16),
                    ),
                  ),
                  Checkbox(
                    value: isSelectWx,
                    onChanged: (changed) {
                      setState(() {
                        isSelectWx = true;
                        isSelectZfb = false;
                      });
                    },
                  )
                ],
              ),
            ),
            onTap: () {
              setState(() {
                isSelectWx = true;
                isSelectZfb = false;
              });
            },
          ),
          InkWell(
            child: Container(
              margin: EdgeInsets.only(
                  left: ScreenAdapter.width(15),
                  right: ScreenAdapter.width(15)),
              decoration: const BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.all(Radius.circular(10)),
              ),
              child: Row(
                children: [
                  Container(
                    width: ScreenAdapter.height(40),
                    height: ScreenAdapter.height(40),
                    padding: EdgeInsets.all(ScreenAdapter.height(10)),
                    child: Image.asset("images/ic_zfb.png", fit: BoxFit.cover),
                  ),
                  const Expanded(
                    child: Text(
                      "支付宝支付",
                      textAlign: TextAlign.left,
                      style: TextStyle(fontSize: 16),
                    ),
                  ),
                  Checkbox(
                    value: isSelectZfb,
                    onChanged: (changed) {
                      setState(() {
                        isSelectWx = false;
                        isSelectZfb = true;
                      });
                    },
                  )
                ],
              ),
            ),
            onTap: () {
              setState(() {
                isSelectWx = false;
                isSelectZfb = true;
              });
            },
          ),
          InkWell(
            child: Container(
              height: ScreenAdapter.height(45),
              margin: EdgeInsets.only(
                top: ScreenAdapter.height(50),
                left: ScreenAdapter.width(15),
                right: ScreenAdapter.width(15),
              ),
              decoration: const BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(12)),
                color: Colors.blue,
              ),
              alignment: Alignment.center,
              child: Text("支付",
                  style: TextStyle(
                      color: Colors.white,
                      fontSize: ScreenAdapter.fontSize(18))),
            ),
            onTap: () {
              if (isSelectWx) {
                ToastMsg.show("微信支付");
              } else {
                ToastMsg.show("支付宝支付");
                AlipayKitPlatform.instance.pay(
                  orderInfo: jsonDecode(jsonString)["data"]["body"],
                );
              }
            },
          )
        ],
      ),
    );
  }
}
